package com.tianqu.tms.www.settle.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;

public class TransferStatisticsMonthExportAction extends TmsAction {

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		//根据userID查询导出
		String acceptMonth = request.getParameter("acceptMonth");
				
        // 第一步，创建一个webbook，对应一个Excel文件  
        HSSFWorkbook wb = new HSSFWorkbook();  
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet  
        HSSFSheet sheet = wb.createSheet("中协数据");  
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
        HSSFRow row = sheet.createRow((int) 0);  
        // 第四步，创建单元格，并设置值表头 设置表头居中  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 创建一个居中格式  
        style.setBorderBottom((short) 1);   
        style.setBorderTop((short) 1);
        style.setBorderLeft((short) 1);
        style.setBorderRight((short) 1); 
        
        sheet.setDefaultRowHeightInPoints(10);
        sheet.setDefaultColumnWidth(20);
        
        HSSFFont  font  =  wb.createFont(); 
        font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        font.setFontHeightInPoints((short)8);
        style.setFont(font);
        
        HSSFCellStyle style1 = wb.createCellStyle();  
        style1.setAlignment(HSSFCellStyle.ALIGN_RIGHT); // 创建一个居中格式  
        style1.setBorderBottom((short) 1);   
        style1.setBorderTop((short) 1);
        style1.setBorderLeft((short) 1);
        style1.setBorderRight((short) 1); 
                
        HSSFFont  font1  =  wb.createFont(); 
        font1.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        font1.setFontHeightInPoints((short)8);
        style1.setFont(font1);
          
        HSSFCellStyle cellStyle = wb.createCellStyle();  
		HSSFFont titleFont = wb.createFont();
		titleFont.setFontName("宋体");
		titleFont.setFontHeight((short) 200);
		titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        
        cellStyle.setFillForegroundColor(HSSFColor.LIGHT_BLUE.index);
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
        cellStyle.setBorderBottom((short) 1);   
        cellStyle.setBorderTop((short) 1);
        cellStyle.setBorderLeft((short) 1);
        cellStyle.setBorderRight((short) 1); 
        cellStyle.setFont(titleFont);
        HSSFCell cell = row.createCell((short) 0);  
        cell.setCellValue("车架号/车辆识别代码");  
        cell.setCellStyle(cellStyle);  

        HSSFCell cell1 = row.createCell((short) 1);  
        cell1.setCellValue("厂牌型号");  
        cell1.setCellStyle(cellStyle); 
        
        HSSFCell cell2 = row.createCell((short) 2);  
        cell2.setCellValue("转入地车辆管理所名称");  
        cell2.setCellStyle(cellStyle); 
        HSSFCell cell3 = row.createCell((short) 3);  
        cell3.setCellValue("车价合计（小写）");  
        cell3.setCellStyle(cellStyle); 
        HSSFCell cell4 = row.createCell((short) 4);  
        cell4.setCellValue("车辆类型");  
        cell4.setCellStyle(cellStyle); 
        
        TransferAcceptedInfo accept = new TransferAcceptedInfo();
        List<TransferAcceptedInfo> list = accept.getAcceptInfoByJYMonth(acceptMonth);
        Codes code = Codes.getInstance();
        if(list!=null)
        {
            for(int i=0;i<list.size();i++)
            {
            	TransferAcceptedInfo entity = (TransferAcceptedInfo)list.get(i);
            	row = sheet.createRow(i+1);
            	HSSFCell c0 = row.createCell((short) 0);
            	c0.setCellStyle(style);
            	c0.setCellValue(entity.getVin());  
            	HSSFCell c1 = row.createCell((short) 1);
            	c1.setCellStyle(style);
            	HSSFCell c4 = row.createCell((short) 4);
            	c4.setCellStyle(style);
            	if(entity.getMarketType() == 1)
            	{
            		Car car = new Car();
            		car = car.findCarBySerial(entity.getBasicCarId());
            		c1.setCellValue(car.getVendorCode()+car.getModelCode()); 
            		c4.setCellValue(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_TYPE, car.getCarType())); 
            		
            	}
            	if(entity.getMarketType() == 2)
            	{
            		TransferPersonalCarInfo person = new TransferPersonalCarInfo();
            		person = person.getPersonalCarInfoBySerial(entity.getBasicCarId());
            		c1.setCellValue(person.getVendorCode()+person.getModelCode()); 
            		c4.setCellValue(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_TYPE, person.getCarType())); 
            	}
    	        HSSFCell c2 = row.createCell((short) 2);
    	        c2.setCellStyle(style);
    	        c2.setCellValue(entity.getCarMarketIn());
    	        HSSFCell c3 = row.createCell((short) 3);
    	        c3.setCellStyle(style1);
    	        c3.setCellValue(entity.getPrice());
            }
        }
        try  
        {  
        	File file =new File(toUtf8String("中协数据"+acceptMonth+".xls"));
        	OutputStream  fout = new FileOutputStream(file);  
            wb.write(fout); 
            getResponse(file, response);
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        } 
		return null;
	}
}